<template>
  <div style="width: 1200px">
    <el-row>
      <el-col :span="10">
        <div class="grid-content bg-purple" style="height: 600px;">
          <el-upload
              class="avatar-uploader"
              action="http://localhost:8081/common/uploadPicture"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
              :before-upload="beforeAvatarUpload">
            <img v-if="form.avatar" :src="form.avatar" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
          <div style="font-size: 20px;font-weight: lighter">
            只支持image和jpeg格式
          </div>
        </div>
      </el-col>
      <el-col :span="14">
        <div class="grid-content bg-purple-light" style="height: 600px">
          <el-form ref="form" :model="form" label-width="80px" style="padding: 40px">
            <el-form-item label="昵称">
              <el-input v-model="form.username"></el-input>
            </el-form-item>
            <el-form-item label="性别">
              <el-radio-group v-model="form.gender">
                <el-radio label="男"></el-radio>
                <el-radio label="女"></el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item label="出生日期">
              <el-date-picker
                  v-model="form.birthday"
                  type="date"
                  placeholder="选择日期">
              </el-date-picker>
            </el-form-item>
            <el-form-item label="兴趣爱好">
              <el-input type="textarea" v-model="form.hobby"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="onSubmit">保存</el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-col>
    </el-row>
  </div>
</template>

<script>
  export default {
    name: "InformationView",
    data() {
      return {
        imageUrl: void 0,
        form: {
          avatar: '',
          username: '',
          birthday: '',
          gender: '',
          hobby: ''
        },
      }
    },
    created() {
      this.getUserInformation()
    },
    methods: {
      handleAvatarSuccess(res,file) {
        this.form.avatar = res.msg;
        this.imageUrl = URL.createObjectURL(file.raw);
      },
      beforeAvatarUpload(file) {
        const isJPG = file.type === 'image/jpeg';
        const isLt2M = file.size / 1024 / 1024 < 2;

        if (!isJPG) {
          this.$message.error('上传头像图片只能是 JPG 格式!');
        }
        if (!isLt2M) {
          this.$message.error('上传头像图片大小不能超过 2MB!');
        }
        return isJPG && isLt2M;
      },
      getUserInformation(){
        this.axios.get("/web/user/getUserInformation").then(response=>{
          this.form.avatar=response.data.data.avatar;
          this.form.birthday=response.data.data.birthday;
          this.form.gender=response.data.data.gender;
          this.form.hobby=response.data.data.hobby;
          this.form.username=response.data.data.username;
        })
      },
      onSubmit(){
        this.axios.post("/web/user/information",this.form).then(response=>{
          if (response.data.code == 200) {
            this.$message({
              message: '恭喜你，这是一条成功消息',
              type: 'success'
            });
          }
        })
      }
    }
  }
</script>

<style scoped>
  .el-row {
    margin-bottom: 20px;
  }
  .el-col {
    border-radius: 4px;
  }
  .bg-purple-dark {
    background: #99a9bf;
  }
  .bg-purple {
    background: #d3dce6;
  }
  .bg-purple-light {
    background: #e5e9f2;
  }
  .grid-content {
    border-radius: 4px;
    min-height: 36px;
  }
  .row-bg {
    padding: 10px 0;
    background-color: #f9fafc;
  }
  /deep/ .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    margin: 50px;
    background-color: #8c939d;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  /deep/ .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  /deep/ .avatar-uploader-icon {
    font-size: 28px;
    color: white;
    width: 230px;
    height: 300px;
    line-height: 300px;
    text-align: center;
  }
  .avatar {
    width: 230px;
    height: 300px;
    display: block;
  }
</style>